package com.example.hand.controller;

import com.example.hand.bean.Admin;
import com.example.hand.bean.PageBean;
import com.example.hand.bean.User;
import com.example.hand.dao.UserDao;
import com.example.hand.service.UserService;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    UserDao userDao;

    @RequestMapping("u_login")//用户登录页面
    public String order(HttpSession session) {

        session.invalidate();
        return "userlogin";
    }

    @RequestMapping(value = "userLogin" ,method = RequestMethod.POST)
    public Object login(String user_name, String user_password, HttpSession session){
        User user;
        user = userService.getLogin(user_name,user_password);
        if(user!=null){
            session.setAttribute("user_name",user_name);
            session.setMaxInactiveInterval(-1);
            return "redirect:userInfo";
        }else {

            session.setAttribute("msg","输入用户名、密码为空或错误！！！");
            return "userlogin";
        }
    }

    @RequestMapping("u_register")
    public String register(HttpSession session){

        session.invalidate();
        return "userregister";
    }

    @RequestMapping("userRegisterSuccess")
    public String registerSuccess(User user, HttpSession session){
        List<User> userList = userService.findByUser(user.getUser_name(),user.getUser_password(),user.getUser_age(),user.getUser_sex());
        if(user.getUser_name()==""){
            session.setAttribute("msg","用户名为空");
            return "userregister";
        }
        if(user.getUser_password()==""){
            session.setAttribute("msg","用户密码为空");
            return "userregister";
        }
        if(user.getUser_age()==""){
            session.setAttribute("msg","用户年龄为空");
            return "userregister";
        }
        if(user.getUser_sex()==""){
            session.setAttribute("msg","用户性别为空");
            return "userregister";
        }
        if(userList != null && userList.size()>0){
                for(User user1 : userList){
                    //判断用户是否存在
                    if(user1.getUser_name().equals(user.getUser_name())){
                        session.setAttribute("msg","用户名重复，请更改用户名");
                        return "userregister";
                    }
                }
            }
            userService.saveUserInfo(user);
            return "userLogin";

    }

    @RequestMapping("userInfo")//用户主页查询
    public String main(Model model, @RequestParam(value="userCurrentPage",defaultValue="1",required=false)int currentPage,User user){
        PageBean<User> pageBean = userService.userPage(currentPage);
        System.out.println(pageBean);
        model.addAttribute("pageBean",pageBean);

        return "userInfo";
    }
    //模糊查询，搜索框
    @RequestMapping(value = "queryUserByLike")
    public String selectUserByLike(HttpServletRequest request, HttpServletResponse response,
                                   HttpSession session, Model model, User user,String content){
        if (content == "") {
            return "redirect:userInfo";
        }
        List<User> userList = userDao.queryUserByLike(content);
        PageBean<User>  pageBean = new PageBean<>();
        pageBean.setLists(userList);
        model.addAttribute("pageBean",pageBean);
        model.addAttribute("user",userList);//这里注意order要和html中的查询遍历那里的名字一样，否则查询框里没有数据。
        return "userInfo";
    }

    @RequestMapping("user_logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "userlogin";
    }
}
